using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class StencilSetter : MonoBehaviour
{
    [SerializeField]
    private int m_StencilRef;

    [SerializeField]
    private CompareFunction m_CompareFunction;

    [SerializeField]
    private StencilOp m_StencilOp;

    private List<Material> m_Materials = new List<Material>();
    private void OnValidate()
    {
        var renderer = this.GetComponent<Renderer>();
        if (renderer != null)
        {
            this.m_Materials.Clear();

            if (Application.isPlaying)
            {
                renderer.GetMaterials(this.m_Materials);
            }
            else
            {
                //renderer.GetSharedMaterials(this.m_Materials);
            }

            foreach (Material mat in this.m_Materials)
            {
                mat.SetFloat("_Stencil", (float)this.m_StencilRef);
                mat.SetFloat("_StencilComp", (float)this.m_CompareFunction);
                mat.SetFloat("_StencilOp", (float)this.m_StencilOp);
            }
        }
    }
}
